Skip to content

C++11 std mutex and condition variable - #1298

Merged
Simn merged 25 commits into
HaxeFoundation:masterfrom
Aidan63:std-synchronisation
Jan 28, 2026
Merged

C++11 std mutex and condition variable#1298
Simn merged 25 commits into
HaxeFoundation:masterfrom
Aidan63:std-synchronisation

Conversation

@Aidan63

@Aidan63 Aidan63 commented Jan 24, 2026

Copy link
Copy Markdown
Contributor

Replaces the old implementation which had a bunch of ifdefs depending on the platform. Now we have a single implementation using the C++ std types. I would have done the same for Lock but for some reason it took until C++20 to get a semaphore type...

I have broken the definitions out into their own headers and exposed the classes so on the haxe side we can remove the untyped code.

Comment thread src/hx/Thread.cpp



Dynamic __hxcpp_mutex_create()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we omit these for hxcpp_api_level >= 500? It would be good to deprecate them anyway since identifiers containing __ are reserved by c++.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do this but in a separate PR, other wise I think we end up in a situation where the CIs need two sets of changes merged at the same time to go green.

Comment thread src/hx/Thread.cpp
}


HxMutex mMutex;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the mutex no longer uses HxMutex, I think we should also remove the HxMutex type from everywhere else in the codebase for internal consistency.

That also officially breaks the HX_THREAD_H_OVERRIDE feature, so we would have to remove that too, or continue to have a HxMutex wrapper.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think replacing uses of HxMutex with std::recursive_mutex makes sense. As a replacement for HX_THREAD_H_OVERRIDE maybe have a hxml & xml HX_THREAD_API_XML define which you can point to a custom xml file which will provide the implementation of the ConditionVariable and RecursiveMutex types?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable, though if it is for hxml use it should be prefixed with HXCPP_ for consistency.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's only one use of HxMutex / a bit of manual pthread condition left in Immix.cpp now, unfortunately this last one is a bit of a pain. C++ states that when the dtor of std::condition_variable gets called no thread should have it locked, however on exit hxcpp programs hang when adapting the last uses of pthread conditions to std::condition_variable as some thread seems to be holding onto the locks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've solved that, the only remaining use of HxMutex is in the Deque and Lock implementation. These two also seem a bit tricky as there is differing implementations for Windows and non Windows platforms (not sure why).
Think I'll also try and get those sorted in a different PR.

Comment thread include/hx/thread/RecursiveMutex.hpp
Comment thread src/hx/thread/ConditionVariable.cpp Outdated
Comment thread src/hx/thread/ConditionVariable.cpp Outdated
Comment thread src/hx/Thread.cpp
return cond->TimedWait(timeout);
bool __hxcpp_condition_timed_wait(Dynamic inCond, double timeout)
{
return hx::Throw(HX_CSTRING("Not Implemented"));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is being removed, I think it would be better to remove it completely rather than keeping it and having a runtime error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking was I'd get rid of it along with the other functions after it's merged in so the CI on the haxe side can stay green with the pending PR.

@Simn

Simn commented Jan 28, 2026

Copy link
Copy Markdown
Member

Let me know when this is good to merge, I'd like to get hxcoro back to hxcpp master soon to see if we benefit from the pch change!

@Aidan63

Aidan63 commented Jan 28, 2026

Copy link
Copy Markdown
Contributor Author

I think it's good to go, the remaining things mentioned are best done in a separate PR after the haxe side is merged so we can keep the CIs green.

@Simn

Simn commented Jan 28, 2026

Copy link
Copy Markdown
Member

Alright! Any change to Immix.cpp terrifies me, but if something comes up I'm sure we (= you) will figure it out!

@Simn
Simn merged commit 0e9183d into HaxeFoundation:master Jan 28, 2026
120 checks passed
@Aidan63
Aidan63 deleted the std-synchronisation branch January 30, 2026 22:11
ionosoft pushed a commit to ionosoft/hxcpp-ion that referenced this pull request Mar 9, 2026
Picks up 37 upstream commits since our last merge (Sep 2, 2025), including:
- Unconditional HXCPP_ALIGN_ALLOC (required for std::atomic in GC objects)
- Missing GCFreeZone calls for semaphores/condvars (HaxeFoundation#1296)
- C++11 std::mutex and condition variable for GC (HaxeFoundation#1298)
- Callable objects with GC write barriers (HaxeFoundation#1047)
- Root handle API (GCAddRoot/GCRemoveRoot) (HaxeFoundation#1292)
- Fix undefined behaviour in View::tryCopyTo overlap (HaxeFoundation#1287)
- Fix static target null Int problem
- Skip splicing when no changes (HaxeFoundation#1309)

Our ionosoft-specific changes preserved:
- GcRegCapture RSI register fix (d71061e)
- msvc-toolchain.xml debug build flags

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
justin-espedal added a commit to Stencyl/hxcpp that referenced this pull request Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants